<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #user_ids="{ record }">
        <!-- 对标题加上 tag -->
        <a-tag style="margin-left:1%;" v-for="(item , index) in record.users" color="blue">{{ item.nickname }}</a-tag>
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import settingMessage from '@/api/setting/settingMessage'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()


const numberOperation = (newValue, id, numberName) => {
  settingMessage.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  settingMessage.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'flyos_setting_message',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: settingMessage.getList,
  recycleApi: settingMessage.getRecycleList,
  add: {
    show: true,
    api: settingMessage.save,
    auth: ['setting:message:save']
  },
  edit: {
    show: true,
    api: settingMessage.update,
    auth: ['setting:message:update']
  },
  delete: {
    show: true,
    api: settingMessage.deletes,
    auth: ['setting:message:delete'],
    realApi: settingMessage.realDeletes,
    realAuth: ['setting:message:realDeletes']
  },
  recovery: {
    show: true,
    api: settingMessage.recoverys,
    auth: ['setting:message:recovery']
  },
  import: {
    show: true,
    url: 'setting/message/import',
    templateUrl: 'setting/message/downloadTemplate',
    auth: ['setting:message:import']
  },
  export: {
    show: true,
    url: 'setting/message/export',
    auth: ['setting:message:export']
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    align:'center',
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入ID"
    }
  },
  {
    title: "接收人",
    dataIndex: "user_ids",
    align:'center',
    formType: 'select',
      multiple:true,
      dict: {
          // 远程通用接口请求，新版代码生成都有一个 remote 接口
          remote: 'system/user/remote',
          // 指定组件接收的props
          props: { label: 'nickname', value: 'id' },
          // 开启分页
          openPage: false,
          // 远程请求配置项
          remoteOption: {
              // 按用户名排序
              sort: { username: 'desc' }, // 如果不指定排序方式，默认为正序排序
              // 设置查询的字段
              select: [ 'id', 'nickname' ],
              // 设置数据过滤
              filter: {
                wx_user_type:['=',5],
              },
              // 关联模型
              relations: [

              ]
          }
      }
  },
  {
    title: "类型",
    dataIndex: "type",
    formType: "select",
    align:'center',
    search: true,
    dict: {
      name: "message_types",
      props: {
        label: "title",
        value: "key"
      },
      translation: true,
      tagColors:{1:'green',2:'blue'}
    }
  },
  {
    title: "是否开启",
    dataIndex: "is_open",
    formType: "select",
    search: true,
    align:'center',
    multiple: false,
    dict: {
      data: [
        {
          label: "开启",
          value: "1"
        },
        {
          label: "关闭",
          value: "2"
        }
      ],
      translation: true,
      tagColors:{1:'green',2:'red'}
    }
  }
])
</script>
<script> export default { name: 'setting:message' } </script>